﻿#include "HolidayOptionUI.h"
#include "CTPTime.h"
#include "Tools.h"
#include "HolidayOptionDelegate.h"

HolidayOptionUI::HolidayOptionUI(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	initUI();
}

HolidayOptionUI::~HolidayOptionUI()
{
}

void HolidayOptionUI::addOption()
{
	ui.tab_holidayList->insertRow(ui.tab_holidayList->rowCount());
}

void HolidayOptionUI::removeOption()
{
	int pcurRow = ui.tab_holidayList->currentRow();
	if (pcurRow < 0)
	{
		return;
	}
	ui.tab_holidayList->removeRow(pcurRow);
}

void HolidayOptionUI::saveOption()
{
	std::set<CTPTm::DayRange> holidayRange, specialWorkDayRange;
	for (int i = 0; i < ui.tab_holidayList->rowCount(); ++i)
	{
		QString pTag = ui.tab_holidayList->item(i, 0)->text();
		QString pBegin = ui.tab_holidayList->item(i, 1)->text();
		QString pEnd = ui.tab_holidayList->item(i, 2)->text();
		CTPTm::DayRange pRange;
		stringToRange(pRange, pBegin, pEnd);
		if (pTag.compare(QString::fromWCharArray(L"节日")) == 0)
		{
			holidayRange.insert(pRange);
			continue;
		}
		if (pTag.compare(QString::fromWCharArray(L"特殊工作日")) == 0)
		{
			specialWorkDayRange.insert(pRange);
			continue;
		}
	}

	CTPTm::HolidayManager holidayMan;
	holidayMan.SaveHolidayAndSpecialDay(holidayRange, specialWorkDayRange);
}

void HolidayOptionUI::initUI()
{
	CTPTm::HolidayManager holidayMan;
	int pRow = 0;
	ui.tab_holidayList->setRowCount(holidayMan.GetHolidayList().size() + holidayMan.GetSpecialWorkDayList().size());
	for (std::set<CTPTm::DayRange>::const_iterator it = holidayMan.GetHolidayList().begin(); it != holidayMan.GetHolidayList().end(); ++it, ++pRow)
	{
		QTableWidgetItem *nameItem = new QTableWidgetItem(QString::fromWCharArray(L"节日"));
		ui.tab_holidayList->setItem(pRow, 0, nameItem);
		
		QString rangeBegin, rangeEnd;
		rangeToString(*it, rangeBegin, rangeEnd);
		QTableWidgetItem *rangeBeginItem = new QTableWidgetItem(rangeBegin);
		ui.tab_holidayList->setItem(pRow, 1, rangeBeginItem);

		QTableWidgetItem *rangeEndItem = new QTableWidgetItem(rangeEnd);
		ui.tab_holidayList->setItem(pRow, 2, rangeEndItem);
	}

	for (std::set<CTPTm::DayRange>::const_iterator it = holidayMan.GetSpecialWorkDayList().begin(); it != holidayMan.GetSpecialWorkDayList().end(); ++it, ++pRow)
	{
		QTableWidgetItem *nameItem = new QTableWidgetItem(QString::fromWCharArray(L"特殊工作日"));
		ui.tab_holidayList->setItem(pRow, 0, nameItem);

		QString rangeBegin, rangeEnd;
		rangeToString(*it, rangeBegin, rangeEnd);
		QTableWidgetItem *rangeBeginItem = new QTableWidgetItem(rangeBegin);
		ui.tab_holidayList->setItem(pRow, 1, rangeBeginItem);

		QTableWidgetItem *rangeEndItem = new QTableWidgetItem(rangeEnd);
		ui.tab_holidayList->setItem(pRow, 2, rangeEndItem);
	}

	ui.tab_holidayList->setItemDelegate(new HolidayOptionDelegate(this));
}

void HolidayOptionUI::rangeToString(const CTPTm::DayRange &pRange, QString &outRangeBegin, QString &outRangeEnd)
{
	tm rangeBeginTm = Tools::GetDateTimeFrmStamp(pRange.RangeBegin);
	std::string rangeBeginStr = Tools::DateToStr(rangeBeginTm);
	outRangeBegin = QString::fromLocal8Bit(rangeBeginStr.c_str());

	tm rangeEndTm = Tools::GetDateTimeFrmStamp(pRange.RangeEnd);
	std::string rangeEndStr = Tools::DateToStr(rangeEndTm);
	outRangeEnd = QString::fromLocal8Bit(rangeEndStr.c_str());
}

void HolidayOptionUI::stringToRange(CTPTm::DayRange &outRange, const QString &pRangeBegin, const QString &pRangeEnd)
{
	std::string rangeBeginStr = pRangeBegin.toLocal8Bit().data();
	tm rangeBeginTm = Tools::DateStr2TM(rangeBeginStr);
	outRange.RangeBegin = Tools::GetTimeStamp(rangeBeginTm);

	std::string rangeEndStr = pRangeEnd.toLocal8Bit().data();
	tm rangeEndTm = Tools::DateStr2TM(rangeEndStr);
	outRange.RangeEnd = Tools::GetTimeStamp(rangeEndTm);
}
